package com.lk.springboot.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lk.springboot.dao.GameMapper;
import com.lk.springboot.dao.TaskMapper;
import com.lk.springboot.dao.UserMapper;
import com.lk.springboot.pojo.Task;
import com.lk.springboot.pojo.USHave;
import com.lk.springboot.service.PidUtilService;
import com.lk.springboot.service.TaskService;
import com.lk.springboot.utils.RandomUtil;
import com.lk.springboot.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author lk
 * @date 2020/1/11 0011 20:20
 */
@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    GameMapper gameMapper;
    @Autowired
    PidUtilService pidUtilService;

    @Override
    public String receiveAward(String u_id, String t_id) {
        Result result = null;
        //判断是否可以领奖
        TwoSpeed twoSpeed = taskMapper.getSpeed(u_id, t_id);
        if (twoSpeed.getSpeed() == 999) {
            result = new Result(ResultCode.HAD);
            result.setData("已经领过奖了");
        } else if (twoSpeed.getSpeed() >= twoSpeed.getT_speed()) {
            List<JSONObject> list = new ArrayList<>();
            //可以领奖，先做标记
            taskMapper.markAward(u_id, t_id);
            //领取奖励，更新记录
            Task task = taskMapper.getTask(t_id);
            result = new Result(ResultCode.SUCCESS);
            if (task.getT_type().equals("score")) {
                int score = task.getT_score();
                taskMapper.addScore(score, u_id);//加分
                JSONObject returnJson = new JSONObject();
                returnJson.put("now_score", taskMapper.getScore(u_id));
                list.add(returnJson);
            } else if (task.getT_type().equals("photo")) {
                int n = task.getT_sphoto();
                //随机取指定数量的拼图
                RandomUtil randomUtil = new RandomUtil();
                List<GameSP> spList = new ArrayList<>();    // 放有数量为n的碎片id和url的集合
                List pidList = pidUtilService.getActiveRandomPid(u_id, n);      // 只能获取用户已经激活过了的 随机p_id*****
                List rareList = randomUtil.taskRandomGetRares(n);
                //获取碎片列表
                for (int i = 0; i < n; i++) {
                    GameSP theSiteSP = gameMapper.getSmallPhoto(pidList.get(i).toString(), rareList.get(i).toString());
                    spList.add(theSiteSP);//放到集合里面
                    String sp_id = theSiteSP.getSp_id();
                    String s_id = gameMapper.getSitesBySp(sp_id);   //取出拼图对应的的站点id
                    USHave usHave = new USHave(u_id, s_id, sp_id);
                    gameMapper.addPhoto(usHave);                    //插入碎片获得记录的表里
                    JSONObject returnJson = new JSONObject();
                    int sp_id_num = Integer.parseInt(sp_id.replace("sp", "")) % 4;
                    if (sp_id_num == 0)
                        sp_id_num = 4;
                    String sp_name = gameMapper.getSName(sp_id) + "0" + sp_id_num;
                    returnJson.put("sp_name", sp_name);
                    returnJson.put("url", theSiteSP.getUrl());
                    returnJson.put("rare", rareList.get(i).toString());
                    list.add(returnJson);
                }
            }
            result.setData(list);

        } else {
            result = new Result(ResultCode.REFULSE_REQUEST);
            result.setData("任务未完成");
        }
        return JSON.toJSONString(result);
    }

    @Override
    public String getUserTask(String u_id) {
        List<UserTask> userTasks = taskMapper.getUserTask(u_id);
        List<JSONObject> list = new ArrayList<>();
        for (UserTask userTask : userTasks) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("t_id", userTask.getT_id());
            jsonObject.put("t_type", userTask.getT_type());
            jsonObject.put("t_name", userTask.getT_name());
            jsonObject.put("t_detail", userTask.getT_detail());
            jsonObject.put("t_speed", userTask.getT_speed());
            if (userTask.getT_type().equals("score"))
                jsonObject.put("award", userTask.getT_score());
            else if (userTask.getT_type().equals("photo"))
                jsonObject.put("award", userTask.getT_sphoto());
            if (userTask.getSpeed() == 999) {
                jsonObject.put("speed", userTask.getT_speed());
                jsonObject.put("isgot", true);
            } else {
                jsonObject.put("speed", userTask.getSpeed());
                jsonObject.put("isgot", false);
            }
            list.add(jsonObject);
        }
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(list);
        return JSON.toJSONString(result);
    }
}
